﻿using System;
using System.Text;

using LeanEngine.Extensions;
using LeanEngine.Validation;
using LeanEngine.Exception;

namespace LeanEngine.Business.Security.CustomValidation
{
    /// <summary>
    /// Attribute for validate if the date is greater than or equal the today date.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    internal sealed class IsNowOrLateDateAttribute : ValidationAttribute
    {
        #region Constructor Members

        /// <summary>
        /// Initialize a new instance of LeanEngine.Business.Security.CustomValidation.IsNowOrLateDateAttribute class 
        /// with a error message obtained from a resource file.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="resourceType">The System.Type of the resource manager that holds the error message.</param>
        /// <param name="errorKey">The key error message contained in the resource.</param>
        public IsNowOrLateDateAttribute(Type resourceType, string errorKey)
            : base(resourceType, errorKey) { }

        /// <summary>
        /// Initialize a new instance of LeanEngine.Business.Security.CustomValidation.IsNowOrLateDateAttribute class 
        /// with an specific error message.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="errorMessage">The error message displayed for non valid values.</param>
        public IsNowOrLateDateAttribute(string errorMessage)
            : base(errorMessage) { }

        #endregion

        #region ValidationAttribute Members

        /// <summary>
        /// Validates the value according to the validation rule and throws an exception in case of validation failure.
        /// </summary>
        /// <param name="value">The value to be validated.</param>
        /// <exception cref="System.ArgumentException"></exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="LeanEngine.Core.BusinessException"></exception>
        public override bool Validate(object value)
        {
            if (value.IsNull()) throw new ArgumentNullException("value");
            if (value.NotIsTypeOf(typeof(DateTime))) throw new ArgumentException("Value must be a datetime.");

            if (value.ToDateTime().IsLessThan(DateTime.Now.Date))
            {
                throw new BusinessException(this.ErrorMessage);
            }

            return true;
        }

        /// <summary>
        /// Validates the value according to the validation rule and add the error message to the container in case of validation failure.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException"></exception>
        /// <param name="value">The value to be validated.</param>
        /// <param name="errorMessageContainer">The container for all the error messages.</param>
        public override void Validate(object value, StringBuilder errorMessageContainer)
        {
            if (value.IsNull()) throw new ArgumentNullException("value");
            if (value.NotIsTypeOf(typeof(DateTime))) throw new ArgumentException("Value must be a datetime.");

            if (value.ToDateTime().IsLessThan(DateTime.Now.Date))
            {
                errorMessageContainer.Append(this.ErrorMessage);
                errorMessageContainer.Append(Environment.NewLine);
            }
        }

        #endregion
    }
}
